package com.wondersgroup.wuhan_demo.scancer;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wondersgroup.wuhan_demo.R;

import cn.mrobot.pkgcore.cookyplus.barcodescanner.BarcodeScanner;
import cn.mrobot.pkgcore.cookyplus.barcodescanner.BarcodeScannerInitListener;
import cn.mrobot.pkgcore.cookyplus.barcodescanner.ScannerListener;
import cn.mrobot.pkgcore.core.ErrorCode;

/**
 * 条码扫描仪示例
 *
 * @author by devil on 2017/12/15
 * @version 1.0.0
 * @since 1.0.0
 */
public class MBarcodeScannerActivity extends Activity implements View.OnClickListener {
    private TextView mResultTv;
    private BarcodeScanner mBarcodeScanner;
    private ScannerListener mScannerListener = new ScannerListener() {
        @Override
        public void onGetVersion(String version) {
            mResultTv.setText("获取版本信息: " + version);
        }

        @Override
        public void onScanned(String data) {
            Log.i("scann==", data + "");
            mResultTv.setText("扫描成功: " + data);
        }

        @Override
        public void onScannerError(ErrorCode errorCode) {
            mResultTv.setText("扫描失败: " + errorCode.getCode() + " , " + errorCode.getMessage());
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cookyplus_barcode_scanner);
        initView();
        initBarcodeScanner();
//        mBarcodeScanner.open();
    }

    private void initView() {
        mResultTv = (TextView) findViewById(R.id.tv_result);
//        findViewById(R.id.btn_open).setOnClickListener(this);
//        findViewById(R.id.btn_close).setOnClickListener(this);
//        findViewById(R.id.btn_version).setOnClickListener(this);
    }


    private void initBarcodeScanner() {
        mBarcodeScanner = BarcodeScanner.get();
        mBarcodeScanner.setListener(mScannerListener);
        mBarcodeScanner.init(new BarcodeScannerInitListener() {
            @Override
            public void onInitSuccess() {
                toast("条码扫描仪初始化成功!");
            }

            @Override
            public void onInitFail(ErrorCode errorCode) {
                toast("条码扫描仪初始化失败: " + errorCode.getCode() + " , " + errorCode.getMessage());
            }
        });
    }


    @Override
    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.btn_open:
//                mBarcodeScanner.open();
//                break;
//            case R.id.btn_close:
//                mBarcodeScanner.close();
//                break;
//            case R.id.btn_version:
//                mBarcodeScanner.getVersion();
//                break;
//            default:
//                break;
//        }
    }

    private void toast(final String msg) {
        Toast.makeText(MBarcodeScannerActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBarcodeScanner.close();
    }
}
